package com.qdlc.p2p.biz.service.impl;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.AdditionalMovieTicketRuleService;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.AdditionalMovieTicketRuleDao;
import com.qdlc.p2p.dal.dto.AdditionalMovieTicketRule;
import com.qdlc.p2p.dal.dto.Operator;
import com.qdlc.p2p.dal.model.AdditionalMovieTicketRuleModel;

/**
 * Created by liaoxiangkai on 2015/11/24.
 */
@Service("additionalMovieTicketRuleService")
public class AdditionalMovieTicketRuleServiceImpl implements AdditionalMovieTicketRuleService {

    @Resource
    private AdditionalMovieTicketRuleDao additionalMovieTicketRuleDao;

    /**
     * 查询电影票规则列表
     *
     * @return
     */
    @Override
    public PageDataList<AdditionalMovieTicketRuleModel> list(AdditionalMovieTicketRuleModel model) {
        return additionalMovieTicketRuleDao.list(model);
    }

    /**
     * 新增电影票规则
     *
     * @param model
     * @return
     */
    @Override
    public AdditionalMovieTicketRule save(AdditionalMovieTicketRuleModel model) {
        AdditionalMovieTicketRule additionalMovieTicketRule = inverseModel(model);
        additionalMovieTicketRule.setCreator(model.getCreator());
        additionalMovieTicketRule.setCreateTime(model.getCreateTime());
        return additionalMovieTicketRuleDao.save(additionalMovieTicketRule);
    }

    /**
     * 通过id查询电影票规则
     *
     * @param id
     * @return
     */
    @Override
    public AdditionalMovieTicketRule find(long id) {
        AdditionalMovieTicketRule entity = additionalMovieTicketRuleDao.find(id);
        if (null == entity) return null;
        AdditionalMovieTicketRuleModel additionalMovieTicketRuleModel = AdditionalMovieTicketRuleModel.instance(entity);
        //将修改人名字设置给model
        Operator modifier = entity.getModifier();
        if (null != modifier)
            additionalMovieTicketRuleModel.setModifierName(modifier.getUserName());
        //把创建人名字设置给model
        Operator creator = entity.getCreator();
        if (null != creator)
            additionalMovieTicketRuleModel.setCreatorName(creator.getUserName());
        return additionalMovieTicketRuleModel;
    }

    /**
     * 修改电影票规则
     *
     * @param model
     * @return
     */
    @Override
    public AdditionalMovieTicketRule update(AdditionalMovieTicketRuleModel model) {
        AdditionalMovieTicketRule additionalMovieTicketRule = inverseModel(model);
        additionalMovieTicketRule.setId(model.getId());
        AdditionalMovieTicketRule findRule = additionalMovieTicketRuleDao.find(model.getId());
        if (null == findRule) return null;
        additionalMovieTicketRule.setModifier(model.getModifier());
        additionalMovieTicketRule.setModifyTime(model.getModifyTime());
        additionalMovieTicketRule.setCreator(findRule.getCreator());
        additionalMovieTicketRule.setCreateTime(findRule.getCreateTime());
        return additionalMovieTicketRuleDao.update(additionalMovieTicketRule);
    }

    /**
     * 修改电影票规则是否启用
     *
     * @param model
     * @return
     */
    @Override
    public int toggleStatus(AdditionalMovieTicketRuleModel model) {
        return additionalMovieTicketRuleDao.toggleStatus(model);
    }

    /**
     * 将model转成基类
     *
     * @param model
     * @return
     */
    public AdditionalMovieTicketRule inverseModel(AdditionalMovieTicketRuleModel model) {
        AdditionalMovieTicketRule additionalMovieTicketRule = new AdditionalMovieTicketRule();
        BeanUtils.copyProperties(model, additionalMovieTicketRule);
        return additionalMovieTicketRule;
    }
}
